/*
 * @lc app=leetcode.cn id=29 lang=java
 *
 * [29] 两数相除
 */

// @lc code=start
class Solution {
    public int divide(int dividend, int divisor) {
        int result = 0;
        if ( dividend == 0 ){
            return 0;
        }
        if (dividend > 0 && divisor > 0){
            while ( dividend >= divisor){
                dividend -= divisor;
                result++;
            }
        }else if ( (dividend > 0 && divisor < 0) || (dividend < 0 && divisor > 0)){
            if ( dividend < 0){
                if ( dividend == -2147483648){
                    dividend += divisor;
                    result--;
                }
                dividend = Math.abs(dividend);
            }else {
                divisor = Math.abs(divisor);
            }
            while ( dividend >= divisor){
                dividend -= divisor;
                result--;
            }
        }else {
            if ( dividend == -2147483648){
                dividend = 2147483647;
            }
            dividend = Math.abs(dividend);
            divisor = Math.abs(divisor);
            while ( dividend >= divisor){
                dividend -= divisor;
                result++;
                
            }
        }
        return result;
    }
}
// @lc code=end

